﻿@model AntUnion.FrameWork.Model.Models.CrmUser
@{
    ViewData["Title"] = "Create";
    Layout = "~/Areas/Console/Views/Shared/_Layout.cshtml";
}
@section head{
    <script type="text/javascript">
        layui.config({
            base: '../../layuiadmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'table', 'form'], function () {
            var $ = layui.$
                , form = layui.form
                , admin = layui.admin
                , table = layui.table
                , parentFrameIndex = parent.layer.getFrameIndex(window.name); //获取窗口索引;

            /**多语言初始化 */
            top.SetFormPageLanguage(layui);

            $.getJSON("/Console/Roles/GetListJson?userType=1", function (result) {
                lang = top.lang;
                let plat = result.data;
                for (var key in top.lang) {
                    if (top.lang.hasOwnProperty(key)) {
                        if (key === "pleaseSelect") {
                            var text = top.lang[key];
                            $('#roles').append(new Option(text, "0"));
                        }
                    }
                }
                plat.forEach(function (v) {
                    for (var key in top.lang) {
                        if (top.lang.hasOwnProperty(key)) {
                            if (key === v.remark) {
                                var text = top.lang[key];
                                $('#roles').append(new Option(text, v.id));
                            }
                        }
                    }
                });
                layui.form.render("select");
            });

            /* 监听提交 */
            form.on('submit(submit-form)', function (data) {
                if (data.field.LoginName == "") {
                    top.AlertByKey(layui, "enterLoginName", 0);
                    return false;
                }
                if (data.field.UserPwd == "") {
                    top.AlertByKey(layui, "enterLoginPassword", 0);
                    return false;
                }
                if (data.field.rUserPwd == "") {
                    top.AlertByKey(layui, "enterConfirmationPassword", 0);
                    return false;
                }
                if (data.field.roles == "0") {
                    top.AlertByKey(layui, "selectUserRole", 0);
                    return false;
                }
                var regex = /(.+){6,12}$/;
                if (!regex.test(data.field.UserPwd)) {
                    top.AlertByKey(layui, "incorrectPasswordFormat", 0);
                    return false;
                }
                if (!regex.test(data.field.rUserPwd)) {
                    top.AlertByKey(layui, "incorrectPasswordFormat", 0);
                    return false;
                }
                if (data.field.UserPwd !== data.field.rUserPwd) {
                    top.AlertByKey(layui, "passwordInconsistency", 0);
                    return false;
                }
                if (data.field.validCode == "") {
                    top.AlertByKey(layui, "enterVerificationCode", 0);
                    return false;
                }
                var postData = {
                    "LoginName": data.field.LoginName,
                    "Name": data.field.Name,
                    "Remark": data.field.Remark,
                    "LoginPwd": data.field.UserPwd,
                    "userRoles": data.field.roles,
                    "validCode": data.field.validCode
                };
                $.ajax({
                    type: "POST",
                    url: "/Console/CRMAccount/Create",
                    data: postData, // 你的formid
                    async: true,
                    error: function (request) {
                        alert("Connection error");
                    },
                    beforeSend: function (xhr) {
                        layui.layer.load();
                    },
                    success: function (data) {
                        var item = eval(data);
                        if (item.code == 0) {
                            /**提示语弹框 */
                            top.ShowMessageByKey(layui, "submitSuccess", 6, 1000, function () {
                                parent.layer.close(parentFrameIndex);
                                parent.renderTable();
                            });
                        } else {
                            /**提示语弹框 */
                            top.AlertByKey(layui, item.msg, 0);
                        }
                        layer.closeAll('loading');
                    }
                });
                return false;
            });

            form.on('submit(sendValidCode)', function (data) {
                if ($("#LoginName").val() == "") {
                    top.AlertByKey(layui, "enterLoginName", 0);
                    return false;
                }
                const validCode = $("#getValidCode")
                var i = 60
                var email = $("#LoginName").val()
                $.ajax({
                    type: "POST",
                    url: "/Console/CRMAccount/SendValidateCode",
                    data: { email: email, type: 'register' }, // 你的formid
                    error: function (request) {
                        console.log("请求超时");
                    },
                    success: function (res) {
                        if (res.code == 0) {
                            top.ShowMessageByKey(layui, res.msg, 6, 1000);
                            var timer = setInterval(() => {
                                i--
                                validCode.attr("disabled", true)
                                validCode.text(i + "s")
                                if (i === 0) {
                                    clearInterval(timer);
                                    validCode.attr("disabled", false)
                                    validCode.text("发送验证码")
                                }
                            }, 1000)
                        } else {
                            top.ShowMessageByKey(layui, res.msg, 0, 1000);
                        }
                    }
                });
                return false
            });
        });
    </script>
}

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="" lay-filter="component-form-group">
                <div class="layui-form-item">
                    <label class="layui-form-label" lay-event="email">邮箱</label>
                    <div class="layui-input-block">
                        <input type="text" name="LoginName" id="LoginName" placeholder="enterLoginName" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label layui-required" lay-event="role">角色</label>
                    <div class="layui-input-block">
                        <select name="roles" id="roles">
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label" lay-event="fullName">姓</label>
                    <div class="layui-input-block">
                        <input type="text" name="Name" id="Name" placeholder="fullName" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label" lay-event="loginPassword">登录密码</label>
                    <div class="layui-input-inline">
                        <input type="password" name="UserPwd" id="UserPwd" placeholder="enterLoginPassword" class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux" lay-event="incorrectPasswordFormat">请填写6到12位密码</div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label" lay-event="confirmationPassword">确认密码</label>
                    <div class="layui-input-inline">
                        <input type="password" name="rUserPwd" id="rUserPwd" placeholder="enterConfirmationPassword" class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux" lay-event="incorrectPasswordFormat">请填写6到12位密码</div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label" lay-event="validCode">验证码</label>
                    <div class="layui-input-inline">
                        <input type="text" name="validCode" id="validCode" placeholder="enterVerificationCode" class="layui-input">
                    </div>
                    <button type="button" class="layui-btn layui-btn-normal" id="sendValidCode" lay-submit="" lay-filter="sendValidCode">发送验证码</button>
                </div>
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label" lay-event="remark">备注</label>
                    <div class="layui-input-block">
                        <textarea name="Remark" id="Remark" placeholder="remark" class="layui-textarea"></textarea>
                    </div>
                </div>
                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button id="submitForm" class="layui-btn" lay-submit="" lay-filter="submit-form">立即提交</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>